import { Component, OnInit,ViewChild } from '@angular/core';
import {adSystemApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service'
import {ModalDirective} from 'ngx-bootstrap';
import {AddClassify} from './../article-manage';
import {EditClassify} from './../article-manage';

@Component({
  selector: 'app-classify-list',
  templateUrl: 'classify-list.component.html',
  styleUrls: ['classify-list.component.scss'],
  providers: []
})
export class ClassifyListComponent implements OnInit {

  tableData: any;
  categoryID: number;
  addClassify = new AddClassify();
  editClassify = new EditClassify();
  promptType: string = 'success';
  promptMsg: string;
  isShow: boolean = false;

  @ViewChild('addClassifyModal') public addClassifyModal: ModalDirective;
  @ViewChild('editClassifyModal') public editClassifyModal: ModalDirective;
  @ViewChild('confirmDelModal') public confirmDelModal: ModalDirective;
  constructor(private httpService: HttpService) { }

  ngOnInit() {
    this.getList();
  }

  //获取分类列表
  getList(){
    const url = `${adSystemApiUrl}/v1/admin/category/list?`;
    return this.httpService.httpGetAuth(url)
      .subscribe(
        data => {
          this.tableData = data.data;
        }
      )
  }

  //打开弹窗
  openDialog(type: number){
    //新建弹窗
    if (type == 1){
      this.addClassifyModal.show();
    }
  }

  //关闭弹窗
  closeModal(){
    this.addClassifyModal.hide();
    this.editClassifyModal.hide();
    this.confirmDelModal.hide();
  }

  //提交新建分类表单
  submitAddClassifyForm(){
    const url =`${adSystemApiUrl}/v1/admin/category/add`;
    return this.httpService.httpPostAuth(url,this.addClassify).map(data => data)
      .subscribe(
        data => {
          if(data.code == 2000){
            this.closeModal();
            this.promptMsg = '添加分类成功';
            this.isShow = true;
            this.getList();
          }

        }
      )
  }

  // 编辑分类弹窗
  editClassifyDialog(id,name,slug,order){
    this.editClassify.categoryID = id;
    this.editClassify.name = name;
    this.editClassify.slug = slug;
    this.editClassify.order = order;
    this.editClassifyModal.show();
  }

  // 提交修改分类表单
  submitEditClassifyForm(){
    const url =`${adSystemApiUrl}/v1/admin/category/edit`;
    return this.httpService.httpPostAuth(url,this.editClassify).map(data => data)
      .subscribe(
        data => {
          if(data.code == 2000){
            this.closeModal();
            this.promptMsg = '修改分类成功';
            this.isShow = true;
            this.getList();
          }
        }
      )
  }

  //确认删除弹窗
  confirmDelDialog(id: number){
    this.categoryID = id;
    this.confirmDelModal.show();
  }

  // 提交删除分类表单
  submitConfirmDelForm(){
    const url =`${adSystemApiUrl}/v1/admin/category/delete`;
    return this.httpService.httpPostAuth(url,{categoryID: this.categoryID}).map(data => data)
      .subscribe(
        data => {
          if(data.code == 2000){
            this.closeModal();
            this.promptMsg = '删除分类成功';
            this.isShow = true;
            this.getList();
          }
        }
      )
  }

  promptIsShow(event: any): void{
    this.isShow = event;
  }
}
